#!/bin/bash

# Script for changing Numix base folder style

# Copyright (C) 2014
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License (version 3+) as
# published by the Free Software Foundation. You should have received
# a copy of the GNU General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.

# Allows timeout when launched via 'Run in Terminal'

function sucess() { sleep 3; exit 0; }
function gerror() { sleep 5; exit 1; }

if [ -z "$1" ]; then
    runmode=2
else
    case "$1" in
        -c|--colours)
            echo -e \
                "This is a list of currently supported folder\n" \
                "\rcolours that can be used to replace the default.\n\n" \
                "\rdefault  - reverts any previous colour change\n" \
                "\rblue     - 42a5f5\n" \
                "\rbrown    - 8d6e63\n" \
                "\rgreen    - 66bb6a\n" \
                "\rgrey     - bdbdbd\n" \
                "\rorange   - f57c00\n" \
                "\rpink     - f06292\n" \
                "\rpurple   - 7e57c2\n" \
                "\rred      - ef5350\n" \
                "\ryellow   - ffca28\n" \
                "\rstyle{#} - use colours from default style #\n" \
                "\rcustom   - choose your own colours"
            sucess ;;
        -s|--styles)
            echo -e \
                "This is a list of currently supported folder\n" \
                "\rstyles that can be used to replace the default.\n\n" \
                "\r0 - default folder theme (uninstall)\n" \
                "\r1 - the original folder design\n" \
                "\r2 - plain design which matches our Legacy themes\n" \
                "\r3 - tilted design which never made it to production\n" \
                "\r4 - one that launched with the redesign of Circle\n" \
                "\r5 - curvy design which never made it to production\n" \
                "\r6 - the current new design that landed in 2015"
            sucess ;;
        -h|--help)
            echo -e \
                "A script for changing the Numix base folder\n" \
                "\rstyle and colour.\n\n" \
                "\rRunning as root makes the change globally,\n" \
                "\rotherwise it is only made locally. Run as\n" \
                "\rappropriate to your Numix installation.\n\n" \
                "\rUsage: numix-folders [OPTION]\n" \
                "\r  -c, --colours \t List of available colours.\n" \
                "\r  -s, --styles \t\t List of available styles.\n" \
                "\r  -h, --help \t\t Displays this help menu.\n"\
                "\r  -p, --prev \t\t Use previous settings.\n"\
                "\r  -t, --cli \t\t Use script in terminal"
            sucess ;;
        -p|--prev)
                runmode=1;;
        -t|--cli)
                runmode=0;;
        *)
            echo -e \
                "numix-folders: invalid option -- '$1'\n" \
                "\rTry 'numix-folders --help' for more information."
            gerror ;;
    esac
fi

cuser="${SUDO_USER:-$USER}"

config_file="$(eval echo "~$cuser")/.config/numix-folders"
if [ ! -f "$config_file" ];then
  touch "$config_file"
fi

scriptpath=$(dirname "$(readlink -f "$0")")

if [ "$runmode" -eq 2 ]; then
    if [ -d /home/"$cuser"/.local/share/icons/Numix/ ]; then
        dir=/home/"$cuser"/.local/share/icons
    elif [ -d /home/"$cuser"/.icons/Numix ]; then
        dir=/home/"$cuser"/.icons
    elif [ -d /usr/share/icons/Numix/ ]; then
        if [[ $UID -ne 0 ]]; then
            scriptname=$(readlink -f "$0")
            exec gksudo "$scriptname"
        else
            dir=/usr/share/icons
        fi
    else
        "$scriptpath"/gui/error
        exit
    fi
    style=$(sed -n 1p "$config_file")
    colour=$(sed -n 2p "$config_file")
    colour1=$(sed -n 3p "$config_file")
    colour2=$(sed -n 4p "$config_file")
    colour3=$(sed -n 5p "$config_file")
    vars=$("$scriptpath"/gui/gui "$style" "$colour" "#$colour1" "#$colour2" "#$colour3")
    style=$(echo $vars | awk '{print $1}')
    colour=$(echo $vars | awk '{print $2}')
    colour1=$(echo $vars | awk '{print $3}')
    colour2=$(echo $vars | awk '{print $4}')
    colour3=$(echo $vars | awk '{print $5}')
    if [[ -z "$style" ]]; then
        exit
    fi
    if [[ $UID -ne 0 ]]; then
        "$scriptpath"/gui/notification&
    else
        sudo -H -u "$cuser" "$scriptpath"/gui/notification&
    fi

else
    if [ -d /home/"$cuser"/.local/share/icons/Numix/ ]; then
        dir=/home/"$cuser"/.local/share/icons
    elif [ -d /home/"$cuser"/.icons/Numix ]; then
        dir=/home/"$cuser"/.icons
    elif [ -d /usr/share/icons/Numix/ ]; then
        if [[ $UID -ne 0 ]]; then
            echo -e \
                "You appear to have Numix installed globally.\n" \
                "\rPlease run this script again as root."
            gerror
        else
            dir=/usr/share/icons
        fi
    else
        echo -e \
            "You don't appear to have Numix installed! Please\n" \
            "\rinstall it and run this script again."
        gerror
    fi

    exitloop=0
    until [ "$exitloop" -eq 1 ]; do
        case $runmode in
            0)
                read -r -p "Which folder style do you want? " answer
                if [ -z "$answer" ]; then
                    style="0"
                elif [ -d "$scriptpath"/styles/"$answer" ]; then
                    style="$answer"
                else
                    echo -e \
                        "Oops! You've chosen an invalid style number.\n" \
                        "\rRun 'numix-folders --styles' for an option list."
                    gerror
                fi

                read -r -p "Which folder colour do you want? " answer
                answer="$(tr '[:upper:]' '[:lower:]' <<< "$answer")" #convert answer to lowercase
                colour1="000000"
                colour2="000000"
                colour3="000000"
                if [ -z "$answer" ]; then
                    colour="default"
                elif [ "$answer" == "custom" ] || [[ $answer == "style"[1-6] ]]; then
                    colour="custom"

                    styledef=$style
                    if [[ $answer == "style"[1-6] ]]; then
                        styledef=${answer#"style"}
                    fi
                    case "$styledef" in
                        "1")
                            col1def="c4905e"
                            col2def="b07f51"
                            col3def="f9f9f9"
                            ;;
                        "2")
                            col1def="e8b07f"
                            col2def="9e7757"
                            col3def="9e7757"
                            ;;
                        "3")
                            col1def="f2bb64"
                            col2def="ea9036"
                            col3def="b58c4b"
                            ;;
                        "4")
                            col1def="f5c14e"
                            col2def="e9a439"
                            col3def="c79d41"
                            ;;
                        "5")
                            col1def="ffb300"
                            col2def="f57c00"
                            col3def="fff8e1"
                            ;;
                        "6" | "0")
                            col1def="ffa726"
                            col2def="ef6c00"
                            col3def="b17621"
                            ;;
                    esac

                    function checkhex() {
                        if ! [[ $1 =~ ^[0-9A-Fa-f]{6}$ ]]; then
                           echo -e \
                               "Oops! The colour: $1 is an invalid hex value.\n" \
                               "\rBe sure to use valid hex values with six digits (e.g. 000000)."
                           gerror
                        fi
                    }

                    if [[ $answer == "custom" ]]; then
                        if grep -q "replacecolour1" styles/"${style}"/Numix/48/places/custom-folder-video.svg; then
                            read -r -p "Choose primary colour in hex notation (default: $col1def): " colour1
                            if [ -z "$colour1" ]; then
                                colour1=$col1def
                            fi
                            checkhex $colour1
                        fi
                        if grep -q "replacecolour2" styles/"${style}"/Numix/48/places/custom-folder-video.svg; then
                            read -r -p "Choose secondary colour in hex notation (default: $col2def): " colour2
                            if [ -z "$colour2" ]; then
                                colour2=$col2def
                            fi
                            checkhex $colour2
                        fi
                        if grep -q "replacecolour3" styles/"${style}"/Numix/48/places/custom-folder-video.svg; then
                            read -r -p "Choose symbol colour in hex notation (default: $col3def): " colour3
                            if [ -z "$colour3" ]; then
                                colour3=$col3def
                            fi
                            checkhex $colour3
                        fi
                    else
                        colour1=$col1def
                        colour2=$col2def
                        colour3=$col3def
                    fi
                elif [ -d colours/"$answer" ]; then
                    colour="$answer"
                else
                    echo -e \
                        "Oops! You've chosen an invalid colour.\n" \
                        "\rRun 'numix-folders --colours' for an option list."
                    gerror
                fi
                exitloop=1
                ;;
            1)
                echo "Trying to use previously stored settings"
                style=$(sed -n 1p "$config_file")
                colour=$(sed -n 2p "$config_file")
                colour1=$(sed -n 3p "$config_file")
                colour2=$(sed -n 4p "$config_file")
                colour3=$(sed -n 5p "$config_file")
                if [[ "$style" -gt 6 || "$style" -lt 1 ]] || [[ -z $colour ]]; then
                    exitloop=0
                    runmode=0
                    echo "There were errors in your config file!"
                    continue
                fi
                if [ "$colour" == "custom" ]; then
                    if ! [[ "$colour1" =~ ^[0-9A-Fa-f]{6}$ ]] || ! [[ "$colour2" =~ ^[0-9A-Fa-f]{6}$ ]] || ! [[ "$colour3" =~ ^[0-9A-Fa-f]{6}$ ]]; then
                        exitloop=0
                        runmode=0
                        echo "There were errors in your config file!"
                        continue
                    fi
                fi
                exitloop=1
                ;;
        esac
    done
fi

cp -rf "$scriptpath"/styles/"${style}"/Numix/* "${dir}"/Numix/

if [ "$colour" == "custom" ]; then
    find ${dir}/Numix/*/{actions,places}/*custom* \
        -exec sed -i --follow-symlinks "s/replacecolour1/#$colour1/g" {} \; \
        -exec sed -i --follow-symlinks "s/replacecolour2/#$colour2/g" {} \; \
        -exec sed -i --follow-symlinks "s/replacecolour3/#$colour3/g" {} \;
fi

currentcolour=$(readlink ${dir}/Numix/16/places/folder.svg | cut -d '-' -f 1)
links=$(find -L ${dir}/Numix/*/{actions,places} -xtype l)

for link in $links; do
    if [[ $link == *folder_color* ]]; then
        continue
    fi
    newlink=$(readlink "${link}");
    if [[ $newlink == *"$currentcolour"* ]]; then
        newlink=${newlink/${currentcolour}/${colour}}
        ln -sf "${newlink}" "${link}"
    fi
done

chown -R "$cuser" "${dir}"/Numix/
gtk-update-icon-cache -f "${dir}"/Numix/
if [ -d "${dir}"/Numix-Circle/ ]; then
    cp -rH "$scriptpath"/styles/"${style}"/Numix-Circle/* "${dir}"/Numix-Circle/
    chown -R "$cuser" "${dir}"/Numix-Circle/
    gtk-update-icon-cache -f "${dir}"/Numix-Circle/
fi
if [ -d "${dir}"/Numix-Square/ ]; then
    cp -rH "$scriptpath"/styles/"${style}"/Numix-Square/* "${dir}"/Numix-Square/
    chown -R "$cuser" "${dir}"/Numix-Square/
    gtk-update-icon-cache -f "${dir}"/Numix-Square/
fi

echo "Folder change complete!"
{
    echo "$style"
    echo "$colour"
    echo "$colour1"
    echo "$colour2"
    echo "$colour3"
} > "$config_file"
chown -R "$cuser" "$config_file"
sucess
